package com.shigu.admin.manager.actions;

import com.opentae.common.beans.LogUtil;
import com.shigu.admin.common.service.OemSystemService;
import com.shigu.admin.common.vo.OemVO;
import com.shigu.component.shiro.CaptchaUsernamePasswordToken;
import com.shigu.admin.manager.bo.LoginBO;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登陆页面
 * Created by zhaohongbo on 16/5/10.
 */
@Controller
public class LoginAction extends BaseAction{


    private static final Logger log = LogUtil.getLog(LoginAction.class);

    @Autowired
    private OemSystemService oemSystemService;

    /**
     * 登陆页
     * @return
     */
    @RequestMapping(value = "login",method = {RequestMethod.POST,RequestMethod.GET})
    public String login(LoginBO bo, HttpServletRequest request, Model model){
        String loginFtl=loginCommon(null,bo,request,model);
        model.addAttribute("oem",oemSystemService.selDefaultOem());
        return loginFtl==null?"login":loginFtl;
    }

    /**
     * 登陆页
     * @return
     */
    @RequestMapping(value = "{oem}/ologin",method = {RequestMethod.POST,RequestMethod.GET})
    public String oemlogin(LoginBO bo, @PathVariable String oem, HttpServletRequest request, Model model){
        OemVO oemObj=(oem==null?oemSystemService.selDefaultOem():oemSystemService.selOemByTag(oem));
        String loginFtl=loginCommon(oemObj.getTag(),bo,request,model);
        model.addAttribute("oem",oemObj);
        return loginFtl==null?"login":loginFtl;
    }

    /**
     * 登录共享
     * @param bo
     * @param request
     * @param model
     * @return
     */
    private String loginCommon(String oemtag,LoginBO bo, HttpServletRequest request, Model model){
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()){//如果已经登陆，去登陆页面
            return "redirect:/index.htm";
        }else if(bo.getUsername()!=null&&bo.getPassword()!=null){
            CaptchaUsernamePasswordToken token = new CaptchaUsernamePasswordToken(oemtag,
                    bo.getUsername(), bo.getPassword(), false, request.getRemoteAddr(), "");
            token.setRememberMe(true);
            try {
                currentUser.login(token);
                currentUser.hasRole("sup");
                //登陆成功
                return "redirect:/index.htm";
            } catch (AuthenticationException e) {
                //登陆失败
                token.clear();
                String msg="账号或密码错误";
                model.addAttribute("msg",msg);
            }
        }
        return null;
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        return "redirect:login.htm";
    }
}
